﻿using System.IO;
using System.Threading.Tasks;
using UnityEngine;

public class ReadAssetHelper
{
    private static readonly AndroidJavaClass ajc_UnityPlayer = null;
    private static readonly AndroidJavaObject currentActivity = null;

    const string javaPath = "unity.tools.ReadAsset";
    private static AndroidJavaClass ajc_SDKCall = null;

    static ReadAssetHelper()
    {
        //获取context
        ajc_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        currentActivity = ajc_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

        Init();
    }

    private static void Init()
    {
        CheckSDK();
        ajc_SDKCall.CallStatic("init", currentActivity);
    }

    public static bool IsFileExists(string path)
    {
        CheckSDK();
        return ajc_SDKCall.CallStatic<bool>("isFileExists", path);
    }

    public static byte[] ReadFile(string path)
    {
        CheckSDK();
        return ajc_SDKCall.CallStatic<byte[]>("readFile", path);
    }

    public static string ReadString(string path)
    {
        CheckSDK();
        return ajc_SDKCall.CallStatic<string>("readString", path);
    }

    private static void CheckSDK()
    {
        if (ajc_SDKCall == null) ajc_SDKCall = new AndroidJavaClass(javaPath);
    }
}
